package com.ms.p2p.controller.admin;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.utils.MsDateUtils;
import com.ms.common.utils.excel.ExcelData;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.provider.DictionaryProvider;
import com.ms.p2p.provider.LoanStayProvider;
import com.ms.p2p.utils.JsonDataGridHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 还款管理
 */
@Controller("AdminLoanRepayController")
@RequestMapping({ "/admin/repayment" })
public class AdminLoanRepayController extends AdminBaseController {

	@Reference
	private LoanStayProvider loanStayProvider;

	@Reference
	private DictionaryProvider dictService;


	/**
	 * 查看不同还款状态下的还款信息
	 *
	 * @param request
	 * @param session
	 * @return
	 * @author:zhangkelong
	 * @date:2015-3-30上午9:41:55
	 */
	@RequestMapping({ "/queryData" })
	@ResponseBody
	public PageBean queryData(HttpServletRequest request, HttpSession session,
							  @RequestParam(value = "userId",required = false) String userId,
							  @RequestParam(value = "userName",required = false) String userName,
							  @RequestParam(value = "borrowId",required = false) String borrowId,
							  @RequestParam(value = "status",required = false) String status,
							  @RequestParam(value = "borrowName",required = false) String borrowName,
							  @RequestParam(value = "begin_date",required = false) String begin_date,
							  @RequestParam(value = "end_date",required = false) String end_date,
							  @RequestParam(value = "currentPage",required = false) String currentPage
	) {
		PageBean page = new PageBean();
		if (currentPage != null && !(("").equals(currentPage))) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		page.setList(loanStayProvider.findPager(userId,userName,borrowId,status,borrowName,begin_date,end_date, page));
		page.setTotalCount(loanStayProvider.findCount(userId,userName,borrowId,status,borrowName,begin_date,end_date));
		return JsonDataGridHelper.createJSONData(page);
	}

	/**
	 * 查看不同还款状态下的还款信息
	 *
	 * @param request
	 * @param session
	 * @return
	 * @author:zhangkelong
	 * @date:2015-3-30上午9:41:55
	 */
	@RequestMapping({ "/list" })
	public String list(HttpServletRequest request, HttpSession session
	) {

		return "admin/borrow/repayment/detail/list";
	}

	@RequestMapping({ "/exportRepaymentManage" })
	public String exportRepaymentManage(HttpServletRequest request, Model model,
										@RequestParam(value = "userId",required = false) String userId,
										@RequestParam(value = "userName",required = false) String userName,
										@RequestParam(value = "borrowId",required = false) String borrowId,
										@RequestParam(value = "status",required = false) String status,
										@RequestParam(value = "borrowName",required = false) String borrowName,
										@RequestParam(value = "begin_date",required = false) String begin_date,
										@RequestParam(value = "end_date",required = false) String end_date
	) {
		PageBean page = new PageBean();
		page.setPageSize(null);
		List<Map<String, Object>> repayments = loanStayProvider.findPager(userId,userName,borrowId,status,borrowName,begin_date,end_date,page);
		for (Map<String, Object> map : repayments) {
			Object account = map.get("account");
			if (account != null) {
				map.put("account", account.toString());
			}
			Object repayment_account = map.get("repayment_account");
			if (repayment_account != null) {
				map.put("repayment_account", repayment_account.toString());
			}
			Object repayment_yesaccount = map.get("repayment_yesaccount");
			if (repayment_yesaccount != null) {
				map.put("repayment_yesaccount", repayment_yesaccount.toString());
			}

			Object style = map.get("style");
			if (style != null) {
				map.put("style", dictService.getDictValue("borrow_style",style+""));
			}
			String r_stay_time = (String) map.get("r_stay_time");
			if (r_stay_time != null) {
				map.put("r_stay_time", MsDateUtils.TimeStamp2Date(r_stay_time));
			}
			Object r_stay_periods = map.get("r_stay_periods");
			Object repayment_periods = map.get("repayment_periods");
			if (r_stay_periods != null && repayment_periods != null) {
				map.put("r_stay_periods", r_stay_periods.toString() + "/" + repayment_periods.toString());
			}
			Object r_stay_account = map.get("r_stay_account");
			if (r_stay_account != null) {
				map.put("r_stay_account", r_stay_account.toString());
			}
			Object r_stay_capital = map.get("r_stay_capital");
			if (r_stay_capital != null) {
				map.put("r_stay_capital", r_stay_capital.toString());
			}
			Object r_stay_interest = map.get("r_stay_interest");
			if (r_stay_interest != null) {
				map.put("r_stay_interest",r_stay_interest.toString());
			}
			Integer r_stay_status = (Integer)map.get("r_stay_status");
			if (r_stay_status != null && r_stay_status == 0) {
				map.put("r_stay_status","未还");
			} else {
				map.put("r_stay_status","已还");
			}

		}
		ExcelData excelData = new ExcelData();
		excelData.setHeads(new String[] { "用户ID", "用户名称", "项目ID","项目名称",
				"项目编号", "借款金额", "还款方式", "应还总额", "已还金额", "还款编号",
				"应还时间", "期数", "本次应还总额", "本次应还本金", "本次应还利息", "还款状态"});
		excelData.setFieldNames(new String[] { "u_id", "u_username", "id",
				"name", "number_id", "account", "style", "repayment_account"
				, "repayment_yesaccount", "r_id", "r_stay_time", "r_stay_periods", "r_stay_account"
				, "r_stay_capital", "r_stay_interest", "r_stay_status"});
		excelData.setDataList(repayments);
		excelData.setModelClass(Map.class);
		model.addAttribute("excelData", excelData);
		model.addAttribute("fileName", "还款明细/记录");

		return "excel";
	}


	@Override
	public boolean getBinderFlag() {
		return true;
	}
}
